﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HPSO;
using System.Configuration;
using System.IO;

namespace QAP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        BackgroundWorker HSPSObw;
        // the HPSO class, the herirachal particle swarm optmization.
        HPSO.HSPO HPSOSystem;
        int counter = 0;
        int fileCounter = 0;
        int caseSingle = 0;
        int fileCount = 0;
        string m_fileName;
        string m_folder;
        /// <summary>
        /// this function gets the data from the configure file.
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            counter = int.Parse(ConfigurationManager.AppSettings.Get("Counter"));
            fileCounter = int.Parse(ConfigurationManager.AppSettings.Get("FileCounter"));
            caseSingle = int.Parse(ConfigurationManager.AppSettings.Get("Case"));
            fileCount = int.Parse(ConfigurationManager.AppSettings.Get("FileNumber"));
            m_folder = ConfigurationManager.AppSettings.Get("Directory");
            m_folder += "\\Results" + DateTime.Now.ToLongDateString();
            Run();
        }
        /// <summary>
        /// This function starts the background worker and thread to start running the files.
        /// </summary>
        public void Run()
        {
            listBox1.Items.Add("Started");
            m_fileName = ConfigurationManager.AppSettings.Get("Directory") + ConfigurationManager.AppSettings.Get("File" + fileCounter);
            listBox1.Items.Add("file Chosen = " + m_fileName + "\t File Counter = " + fileCounter.ToString());
            HSPSObw = new BackgroundWorker();
            switch (caseSingle)
            {
               
                case 0:
                    listBox1.Items.Add("HPSO");
                    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, false, false, true, true, true);
                    HSPSObw.DoWork += HPSOSystem.RunSystem;
                    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                    HSPSObw.RunWorkerAsync();
                    break;
                //case 1:
                //    listBox1.Items.Add("HPSO biased term");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, false, true, false, true, false);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;
                //case 2:
                //    listBox1.Items.Add("HPSO biased term and  single local search");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, false, true, true, true, false);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;
                //case 3:
                //    listBox1.Items.Add("HPSO full local search");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, true, false, false, true, false);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;

                //case 4:
                //    listBox1.Items.Add("HPSO biased term and  full local search");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, true, false, true, true, false);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;

                //case 5:
                //    listBox1.Items.Add("HPSO single tabu search");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, false, false, false, true, true);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;
                //case 6:
                //    listBox1.Items.Add("HPSO biased term and single tabu search");
                //    HPSOSystem = new HSPO(m_folder, HSPSObw, m_fileName, false, false, true, true, true);
                //    HSPSObw.DoWork += HPSOSystem.RunSystem;
                //    HSPSObw.RunWorkerCompleted += bw_RunWorkerCompleted;
                //    HSPSObw.RunWorkerAsync();
                //    break;

                
            }
            
        }

        /// <summary>
        /// after each run this function is to recall the backgroup worker to contino its run.
        /// </summary>
        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!(e.Error == null))
            {
                listBox1.Items.Add("Error: " + e.Error.Message);
                listBox1.Items.Add("Stack: " + e.Error.StackTrace);

                HSPSObw.RunWorkerAsync();


            }
            else
            {
                listBox1.Items.Add("Done! iteration Number " + counter);
                if (counter < 49)
                {

                    HSPSObw.RunWorkerAsync();
                    counter++;

                }
                else
                {
                    if (caseSingle < 6)
                    {
                        caseSingle++;
                        counter = 0;
                        Run();
                    }
                    else
                    {
                        if (fileCounter < fileCount)
                        {
                            caseSingle = 0;
                            counter = 0;
                            fileCounter++;
                            Run();
                        }

                    }
                }
            }
        }

    }
}
